import { createRouter, createWebHistory } from 'vue-router'
// 这个type可加可不加 加了代表这个是类型
import type { RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/cache'
import { firstMenu } from '@/utils/map-menus'

// RouteRecordRaw路由数组类型
const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "login-bunk" */ 'views/login/login.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import(/* webpackChunkName: "main-bunk" */ 'views/main/main.vue')
  },
  {
    path: '/:pathMatch(.*)',
    name: 'notFount',
    component: () => import(/* webpackChunkName: "not-fount-bunk" */ 'views/notFount/notFount.vue')
  }
]

const router = createRouter({
  routes,
  history: createWebHistory()
})

router.beforeEach((to) => {
  if (to.path !== '/login') {
    if (!localCache.getCache('token')) {
      return '/login'
    }
  }

  if (to.path === '/main') {
    return firstMenu.url
  }
})

export default router
